/*
 * This file be made by lanna
 */
package uit.nfcmenu.view;

import java.util.ArrayList;

import uit.nfcmenu.db.model.MenuModel;
import uit.nfcmenu.manager.AssetsManager;
import uit.nfcmenu.util.ImageHelper;
import uit.nfcmenu.util.ImageLoader;
import uit.nfcmenu.util.UiUtilities.ICallBack;
import uit.nfcmenu.webservice.JSONParser;
import uit.nfcmenu.webservice.PreferenceHelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import uit.nfcmenu.R;

public class MenuAdapter extends BaseAdapter {
	static String tag = MenuAdapter.class.getSimpleName();

	Context mContext;
	ArrayList<MenuModel> mModels;
	ICallBack mICallBack;
	String mLanguage;
	ImageLoader mImageLoader;

	// values for view
	int imgW = 0;
	int imgH = 0;
	int cornerRadius = 0;

	public MenuAdapter(Context context, ArrayList<MenuModel> models,
			ICallBack iCallBack) {
		mContext = context;
		mModels = models;
		mICallBack = iCallBack;
		mLanguage = PreferenceHelper.loadLanguage(mContext);
		mImageLoader = new ImageLoader(mContext, false);
		
		imgW = (int) mContext.getResources().getDimension(R.dimen.menu_width);
		imgH = (int) mContext.getResources().getDimension(R.dimen.menu_height);
		cornerRadius = (int) mContext.getResources().getDimension(R.dimen.menu_corner);
	}

	public ArrayList<MenuModel> getmModels() {
		return mModels;
	}

	public void setmModels(ArrayList<MenuModel> mModels) {
		this.mModels = mModels;
	}

	@Override
	public int getCount() {
		return (mModels == null) ? 0 : mModels.size();
	}

	@Override
	public Object getItem(int position) {
		return (mModels == null || (position < 0 && position >= mModels.size())) 
				? null : mModels.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// init view
		ViewHolder holder;
		final LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.inflater_menu, null);
			holder = new ViewHolder();
			holder.btnImage = (ImageView) convertView.findViewById(R.id.img_menu);
			holder.mainLayout = (ViewGroup) convertView.findViewById(R.id.main_layout);
			holder.tvName = (TextView) convertView.findViewById(R.id.tv_menu);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		AssetsManager.setCommonAppFont(mContext, holder.tvName);
		
		// init data
		MenuModel model = mModels.get(position);
		if (model != null) {
			String name = model.getName();
			name = JSONParser.getStringByLanguage(name, mLanguage);
			holder.tvName.setText(name);
			holder.mainLayout.setSelected(isSelectedView(position));
			holder.mainLayout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mICallBack.callBack(position);
				}
			});
			
			// show image in ImageView obj
			if (imgW != 0 && imgH != 0) {
//				Log.i(tag, "setViewWithRoundCorners(): iv w="+imgW+", h="+imgH);
//				ImageHelper.setViewWithRoundCorners(holder.btnImage, model.getBitmap(), 
//						imgW, imgH, cornerRadius);
				ImageHelper.setImageSplitedCenterVertical(holder.btnImage, 
						model.getBitmap(), imgW, imgH);
				
			}
		}
		return convertView;
	}

	class ViewHolder {
		ImageView btnImage;
		ViewGroup mainLayout;
		TextView tvName;
	}
	
	public static boolean isSelectedView(int position) {
		boolean isSelectedView = (position & 1) != 0;
		//Log.i(tag, "isSelectedView="+isSelectedView);
		return isSelectedView; // odd position
	}
}
